using System;
using System.Collections.Generic;
using System.Linq;
using Cysharp.Threading.Tasks;

namespace Processors
{
    /// <summary>
    /// 容器Processor, 用于包含其他子processor
    /// </summary>
    /// <typeparam name="TProcessor"></typeparam>
    public class ContainerProcessor<TProcessor> : IProcessor where TProcessor : IProcessor
    {
        protected readonly List<TProcessor> _processors;

        public ContainerProcessor(IEnumerable<TProcessor> processors)
        {
            if (processors == null || !processors.Any())
                throw new ArgumentNullException(nameof(processors));
            _processors = processors.ToList();
        }

        public async UniTask<object> Process(object input, Context context)
        {
            var output = input;
            foreach (var processor in _processors)
            {
                output = await processor.Process(output, context);
            }

            return output;
        }
    }
}